<template>
  <div>
    <el-card class="box-1">
      <div slot="header"><span>业绩订单修改</span></div>
      <el-form ref="form" :model="form" :rules="formRules" label-width="160px">
        <el-form-item label="客户名称">
          {{ form.member_name }}
        </el-form-item>
        <el-form-item label="客户手机">
          {{ form.member_phone }}
        </el-form-item>
        <el-form-item label="商品名称" prop="goods_name">
          <el-select v-model="form.goods_idx" placeholder="请选择商品" filterable @change="change">
            <el-option v-for="(item, index) in goodsList" :key="index" :label="item.goods_name" :value="index" />
          </el-select>
          <el-select v-if="form.goods_idx!==''&&goodsList[form.goods_idx].goods_version.length>0" v-model="form.goods_version_idx" placeholder="请选择版本" filterable>
            <el-option v-for="(item, index) in goodsList[form.goods_idx].goods_version" :key="index" :label="item.goods_version" :value="index" />
          </el-select>
          <el-select v-if="form.goods_version_idx!==''&&goodsList[form.goods_idx].goods_version[form.goods_version_idx].subitem.length>0" v-model="form.goods_subitem_idx" placeholder="请选择规格" filterable @change="selectGoods">
            <el-option v-for="(item, index) in goodsList[form.goods_idx].goods_version[form.goods_version_idx].subitem" :key="index" :label="item.goods_version" :value="index" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品原价">
          <el-input v-model="form.goods_price" />
        </el-form-item>
        <el-form-item label="商品金额">
          <el-input v-model="form.goods_money" />
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="form.order_number" />
        </el-form-item>
        <el-form-item label="购买时间">
          <el-date-picker v-model="form.buy_time" value-format="yyyy-MM-dd" type="date" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="购买时长">
          <el-input v-model="form.buy_length" />
          <div class="remark">*注:时长是按月计算</div>
        </el-form-item>
        <el-form-item label="售后人员">
          <el-select v-model="form.after_sale_id" placeholder="请选择售后人员" filterable>
            <el-option v-for="item in usersList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remarks" type="textarea" rows="4"/>
        </el-form-item>
        <el-form-item label="拒绝备注">
          <el-input v-model="form.refuse_remarks" type="textarea" rows="4"/>
        </el-form-item>
        <el-form-item label="销售凭证">
          <div class="remark">*注:最多传三张图</div>
          <ws-upload-multiple-img v-model="form['pic_list']" :path="form['pic_list_url']" :limit="3"/>
        </el-form-item>
        <div style="text-align:center">
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // ImageUrl: this.$store.getters.serverUrl,
ImageUrl: '',

      dialogImageUrl: '',
      showImageDialog: false,

      usersList: [], // 系统用户列表
      goodsList: [], // 商品列表

      form: {
        name: '',
        phone: '',
        goods: '',
        price: '',
        order_num: '',
        date: '',
        term: '',
        afterSale: '',
        after_sale_id: '',
        remarks: '',
        sale_proof: '',
        goods_idx: '',
        goods_version_idx: '',
        goods_subitem_idx: ''
      },
      formRules: {
      }
    }
  },
  watch: {
  },
  created() {
    this.id = this.$route.query.id
    this.getData()
    this.getUsersList()
    this.getGoodsList()
  },
  methods: {
    change(){
      this.form.goods_version_idx = '';
      this.form.goods_subitem_idx = '';
    },
    selectGoods() {
      this.form.price = this.goodsList[this.form.goods_idx].goods_version[this.form.goods_version_idx].subitem[this.form.goods_subitem_idx].originalPrice;
      this.form.goods_name = this.goodsList[this.form.goods_idx].goods_name +
        '--' + this.goodsList[this.form.goods_idx].goods_version[this.form.goods_version_idx].goods_version +
        '--' + this.goodsList[this.form.goods_idx].goods_version[this.form.goods_version_idx].subitem[this.form.goods_subitem_idx].goods_version
    },
    getData() {
      const url = 'achievement/detail/' + this.id
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.form = res.data;
        this.form.pic_list = res.data.sale_proof.join(',')
        this.form.pic_list_url = []
        res.data.sale_proof.forEach(item => {
          this.form.pic_list_url.push({ id: item, url: this.ImageUrl + item })
        })
        this.form.goods_idx = '';
        this.form.goods_version_idx = '';
        this.form.goods_subitem_idx = '';
        const temp = this.form.goods_name.split('--');
        for (let i = 0; i < this.goodsList.length; i++) {
          if (temp[0] && this.goodsList[i].goods_name === temp[0]) {
            this.form.goods_idx = i;
            for (let j = 0; j < this.goodsList[i].goods_version.length; j++) {
              if (temp[1] && this.goodsList[i].goods_version[j].goods_version === temp[1]) {
                this.form.goods_version_idx = j;
                for (let k = 0; k < this.goodsList[i].goods_version[j].subitem.length; k++) {
                  if (temp[2] && this.goodsList[i].goods_version[j].subitem[k].goods_version === temp[2]) {
                    this.form.goods_subitem_idx = k;
                  }
                }
              }
            }
          }
        }
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取系统用户列表
     *  接口:'user/all-list'
     */
    getUsersList() {
      const url = 'user/all-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.usersList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取商品列表
     *  接口:'goods/all-list'
     */
    getGoodsList() {
      const url = 'goods/all-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.goodsList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.form
          data.sale_proof = data.pic_list
          const url = 'achievement/edit/' + this.id
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.$message.success(res.msg)
            this.$router.back(-1)
          }).catch((err) => {
            this.$message.error(err.msg)
          })
        } else {
          this.$message.error('提交失败,请检查必填项')
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  max-width: 1000px;
  margin: 0 auto;
  > div span {
    font-size: 18px;
    font-weight: 600;
    padding: 0 10px;
  }
  .el-form {
    max-width: 900px;
    .title {
      font-weight: 600;
    }
    .remark {
      font-size: 13px;
      color: $textGray;
    }
    .el-button {
      @extend .bg-green;
      width: 20%;
      min-width: 160px;
    }
  }
}
.el-checkbox {
  margin-right: 30px;
}
.el-checkbox + .el-checkbox {
  margin-left: 0;
  margin-right: 30px;
}
</style>
